define(function( require ) {
    window.NC = require('core/event');

    var Marionette = require('marionette'),
        modalView = require('components/image/1.0.0/views/image_list_view'),
        ImageCollection = require('components/image/1.0.0/collections/images'),
        ImageDownloadView = require('components/image/1.0.0/views/image_download'),
        ImageDownloadModel = require('components/image/1.0.0/models/image_download')
        ImageUploadView = require('components/image/1.0.0/views/image_upload'),
        ImageUploadCollection = require('components/image/1.0.0/collections/image_upload');

    var app = new Marionette.Application();

    // before app init
    app.on('initialize:before', function() {
        $('body').append('<div class="js-image-app-container"></div>')

        app.addRegions({
            container: '.js-image-app-container',
            imageUpload: '.js-upload-local-img',
            imageDownload: '.js-get-web-img'
        });
    });
    // on Init
    var imageCollection = new ImageCollection(),
        imageDownloadModel = new ImageDownloadModel(),
        imageUploadCollection = new ImageUploadCollection();

    app.addInitializer(function(options) {
        app.container.show(new modalView({
            collection: imageCollection,
            options: app._options
        }));
        if (!app._options.hideDownload) {
            // 下载网络图片
            app.imageDownload.show(new ImageDownloadView({
                model: imageDownloadModel
            }));
        }

        app.imageUpload.show(new ImageUploadView({
            collection: imageUploadCollection
        }));
    });

    app.on('show:image', function() {
        app.container.$el.find('.modal').modal('show');

        var tabIndex = app._options.tabIndex
        , onlyUpload = app._options.onlyUpload
        , tab = app.container.$el.find('.js-modal-tab');
        if(tabIndex != undefined){
            tab.eq(tabIndex).click();
        }

        // TODO 改为 marionette 方式
        var maxSize = app._options.maxSize
        , sizeStr;
        if(maxSize != undefined){
            if(maxSize % 1024 == 0){
                sizeStr = maxSize/1024 + ' MB';
            } else {
                sizeStr = maxSize + ' KB';
            }
            app.imageUpload.$el.find('.js-max-size').html(sizeStr);
        }

        if(onlyUpload === true){
            app.container.$el.find('.module-nav li').eq(0).hide();
            tab.eq(1).click();
        }
    });
    var handleSuccess = function(data, options) {
        if (app._options && _.isFunction(app._options.callback)) {
            app._options.callback(data, options);
        }
        app.container.$el.find('.modal').modal('hide');
    }
    window.NC.on('image:upload:success', handleSuccess);
    window.NC.on('image:download:success', handleSuccess);
    window.NC.on('image:choose:success', handleSuccess);
    window.NC.on('image:number:change', function(){
        if(imageUploadCollection.multiUpload !== true){
            app.imageUpload.$el.find('.fileinput-button').toggle(imageUploadCollection.length === 0);
        }
    });

    return {
        initialize: function(options) {
            app._options = options;
            if (!this._initialized) {
                app.start();
                this._initialized = true;
            }


            if (options.multiChoose === false) {
                imageCollection.multiChoose = false;
            } else {
                imageCollection.multiChoose = true;
            }

            if (options.multiUpload === false) {
                imageUploadCollection.multiUpload = false;
            } else {
                imageUploadCollection.multiUpload = true;
            }

            if (options.uploadURL) {
                imageUploadCollection.uploadURL = options.uploadURL;
            }

            if (options.private) {
                imageUploadCollection.private = true;
            } else {
                imageUploadCollection.private = false;
            }

            if (options.maxSize == undefined) {
                options.maxSize = imageUploadCollection.maxSize = 1024;
                imageCollection.maxSize = 1024;
            } else {
                imageUploadCollection.maxSize = options.maxSize;
                imageCollection.maxSize = options.maxSize;
            }

            app.trigger('show:image');
        }
    };
});
